/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package logic.services;

import com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException;
import edu.fca.beans.ProfesorBean;
import edu.fca.daos.ProfesorDAO;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Romms
 */
public class ProfesorServices {
    private ProfesorDAO profesorDAO;

    private static ProfesorServices instance;

    private ProfesorServices() {
        profesorDAO=new ProfesorDAO();
    }

    public static ProfesorServices getInstance(){
        if(instance==null){
            instance = new ProfesorServices();
        }
        return instance;
    }

    public void addProfesor(ProfesorBean profesor) throws MySQLIntegrityConstraintViolationException{
        try {
            profesorDAO.add(profesor);
        } catch (MySQLIntegrityConstraintViolationException ex) {
            throw ex;
        }catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public void updateProfesor(ProfesorBean newProfesor, String lastCode) throws MySQLIntegrityConstraintViolationException{
        try {
            profesorDAO.updateWhere(newProfesor, "codigo='"+lastCode+"'");
        } catch (MySQLIntegrityConstraintViolationException ex) {
            throw ex;
        }catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    
    public void deleteProfesor(ProfesorBean profesor) throws MySQLIntegrityConstraintViolationException{
        try {
            profesorDAO.deleteWhere("codigo=" +profesor.getCodigo() );
        } catch (MySQLIntegrityConstraintViolationException ex) {
            throw ex;
        }catch (Exception ex) {
            ex.printStackTrace();
        }
    }



    public ArrayList<ProfesorBean> getProfesores(){
        try {
            return profesorDAO.selectAll();
        } catch (Exception ex) {
            Logger.getLogger(ProfesorServices.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
    }

}
